﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExpertMIS.Brokers;
using ExpertMIS.Entities;
using System.Collections;

namespace ExpertMIS.Admin.UserControl
{
    public partial class ArticleForm : System.Web.UI.UserControl
    {
        #region 控件事件处理

        protected void Page_Load(object sender, EventArgs e)
        {
            fckContent.BasePath = "~/Admin/UserControl/FCKEditor/";

            if (!IsPostBack)
            {
                RefreshArticleColumn();
                rblColumns.Font.Size = 9;
            }
        }

        #endregion

        #region 公有方法


        public ADMArticleEntity Entity
        {
            get
            {
                return ViewState["_ADMArticleEntity_"] as ADMArticleEntity;
            }
            set
            {
                ViewState["_ADMArticleEntity_"] = value;
            }
        }

        public void LoadArticle(string articleOID)
        {
            ADMArticleBroker broker = new ADMArticleBroker();

            Hashtable htQuery = new Hashtable();
            htQuery["OID"] = articleOID;
            Entity = broker.GetEntity(htQuery) as ADMArticleEntity;

            if (Entity == null)
            {
                Entity = broker.NewEntity() as ADMArticleEntity;
            }
            else
            {
                txTitle.Text = Entity.Title;

                RefreshArticleColumn();
                for(int i=0; i<= rblColumns.Items.Count; i++)
                {
                    ListItem item = rblColumns.Items[i];
                    if (item.Value == Entity.ColumnID)
                    {
                        rblColumns.SelectedIndex = i;
                        break;
                    }
                }

                txSource.Text = Entity.Source;
                txAuthor.Text = Entity.Author;
                fckContent.Value = Entity.Content;
            }
        }

        public bool SaveArticle()
        {
            try
            {
                ADMArticleBroker broker = new ADMArticleBroker();
                Entity.Title = txTitle.Text;
                Entity.Author = txAuthor.Text;
                Entity.ColumnID = rblColumns.SelectedItem.Value;
                Entity.IsPassed = true;
                Entity.PublishedTime = DateTime.Now;
                Entity.Content = fckContent.Value;
                Entity.Source = txSource.Text;
                broker.SaveEntity(Entity, null);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public void RefreshArticleColumn()
        {
            ADMColumnBroker broker = new ADMColumnBroker();
            rblColumns.DataSource = broker.GetAllColumns();
            rblColumns.DataTextField = "Name";
            rblColumns.DataValueField = "OID";
            rblColumns.DataBind();
        }

       
         #endregion
    }
}